package com.floreantpos.model.dao;

import com.floreantpos.model.Guest;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class GuestDAO extends BaseGuestDAO {

  /**
   * Default constructor. Can be used in place of getInstance()
   */
  public GuestDAO() {
  }

  public Guest findGuestByTelephone(String telephone) {
    Session session = null;

    try {
      session = getSession();
      Criteria criteria = session.createCriteria(getReferenceClass());
      criteria.add(Restrictions.eq(Guest.PROP_TELEPHONE, String.valueOf(telephone)));

      List list = criteria.list();
      if (list.size() <= 0) {
        return null;
      }

      return (Guest) list.get(0);
    } finally {
      closeSession(session);
    }
  }
  
  public Guest findGuestByTaxId(String taxId) {
    Session session = null;

    try {
      session = getSession();
      Criteria criteria = session.createCriteria(getReferenceClass());
      criteria.add(Restrictions.eq(Guest.PROP_TAX_ID, String.valueOf(taxId)));

      List list = criteria.list();
      if (list.size() <= 0) {
        return null;
      }

      return (Guest) list.get(0);
    } finally {
      closeSession(session);
    }
  }  
  
  public Guest findGuestByTaxId(String taxId, Boolean isCloseSession) {
    Session session = null;

    try {
      session = getSession();
      Criteria criteria = session.createCriteria(getReferenceClass());
      criteria.add(Restrictions.eq(Guest.PROP_TAX_ID, String.valueOf(taxId)));

      List list = criteria.list();
      if (list.size() <= 0) {
        return null;
      }

      return (Guest) list.get(0);
    } finally {
      if (isCloseSession)
        closeSession(session);
    }
  }  

}
